package com.cn.netty;

import io.netty.channel.*;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.net.SocketAddress;

@Slf4j
public class EmbedHandler {

    @Test
    public void test1() {
        EmbedHandler embedHandler = new EmbedHandler();

//        embedHandler.

        ChannelInboundHandlerAdapter i1 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.debug("i1 ");
                super.channelRead(ctx, msg);
            }

        };

        ChannelInboundHandlerAdapter i2 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.debug("i2 ");

                //channel 继承自
                ChannelFuture hello_wrold = ctx.channel().writeAndFlush("hello wrold");

                super.channelRead(ctx, msg);
            }

        };

        ChannelOutboundHandlerAdapter o1 = new ChannelOutboundHandlerAdapter() {
            @Override
            public void read(ChannelHandlerContext ctx) throws Exception {
                log.debug("o1 read");
                ChannelHandlerContext read = ctx.read();
                super.read(ctx);
            }

            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                log.debug("o1 ");
                super.write(ctx, msg, promise);
            }

        };

        ChannelOutboundHandlerAdapter o2 = new ChannelOutboundHandlerAdapter() {
            @Override
            public void read(ChannelHandlerContext ctx) throws Exception {
                log.debug("o2 read");
                ctx.read();
            }

            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                log.debug("o2 ");
                super.write(ctx, msg, promise);
            }

        };


        EmbeddedChannel embeddedChannel = new EmbeddedChannel();

        embeddedChannel.pipeline().addLast(
                i1,
                i2,
                o1,
                o2
        );

        embeddedChannel.writeInbound("ine");

//        embeddedChannel.writeOneInbound("out");

    }

}
